package com.kreedx.article.factory.pipeline;

import cn.hutool.crypto.digest.MD5;
import com.geccocrawler.gecco.annotation.Gecco;
import com.geccocrawler.gecco.annotation.Href;
import com.geccocrawler.gecco.annotation.HtmlField;
import com.geccocrawler.gecco.annotation.Request;
import com.geccocrawler.gecco.request.HttpGetRequest;
import com.geccocrawler.gecco.request.HttpRequest;
import com.geccocrawler.gecco.scheduler.DeriveSchedulerContext;
import com.geccocrawler.gecco.spider.HtmlBean;
import com.kreedx.article.factory.entity.Article;
import com.kreedx.article.factory.repository.ArticleRepository;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service("articlePipeline")
@Slf4j
public class ArticlePipeline extends BasePipeline<ArticlePipeline.Bean> {

    @Resource
    ArticleRepository articleRepository;

    public final static Set<String> fetchedSet = new HashSet<>();


    @Override
    public void onCapture(Bean bean) {
        log.info(bean.title);
        bean.getUrls().forEach(url -> {
//            Pattern pattern = Pattern.compile("https://www.jianshu.com/p/.*");
            Pattern pattern = Pattern.compile("https://new.qq.com/omn/.*");
            Matcher matcher = pattern.matcher(url);

            if (!matcher.find()) {
                return;
            }
            synchronized (fetchedSet) {
                if (!fetchedSet.contains(url) && fetchedSet.size() < 15) {

                    log.info(url);
                    fetchedSet.add(url);
                    DeriveSchedulerContext.into(new HttpGetRequest(url));
                }
            }


        });
        String id = MD5.create().digestHex(bean.getContent());
        Article article = new Article();
        article.setId(id);
        article.setTitle(bean.getTitle());
        article.setContent(bean.getContent());
        if (articleRepository.findById(id).isPresent()) {
            article.setCreateAt(new Date());
        } else {
            article.setUpdateAt(new Date());
        }
        articleRepository.save(article);

    }

    @Data
    @Gecco(matchUrl = {"*"}, pipelines = "articlePipeline", downloader = "htmlUnitDownloder")
    static public class Bean implements HtmlBean {
        @Request
        HttpRequest request;
        @HtmlField(cssPath = "h1")
        private String title;
        //        @HtmlField(cssPath = "article")
        @HtmlField(cssPath = ".content-article")
        private String content;
        @Href
        @HtmlField(cssPath = "a[href]")
        private List<String> urls;

    }

}
